<?php



namespace App\Model;

use Illuminate\Database\Eloquent\Model;

/**
 *  金币日志记录
 */
class CoinLog extends Model
{
    protected $table = 'coin_log';

    const FLAG_OK = 1;
    const FLAG_ERR = 2;

    //充值
    const TYPE_RECHARGE = 1;
    //消费
    const TYPE_CONSUME = 2;
    //收入
    const TYPE_INCOME = 3;
    const TYPE = [
        self::TYPE_RECHARGE => '充值',
        self::TYPE_CONSUME => '消费',
        self::TYPE_INCOME => '收入',
    ];

    //来源-个人中心充值
    const FROM_RECHARGE = 1;
    //来源-新手福利
    const FROM_NEW = 2;
    //来源-语音电话消费
    const FROM_VOICECALL_OUT = 3;
    //来源-语音电话收入
    const FROM_VOICECALL_IN = 4;
    //来源-活动赠送
    // const FROM_ACT = 5;
    //提现
    const FROM_WITHDRAW = 6;
    //来源-首充赠送
    const FROM_FIRST_RECHARGE = 7;
    //来源-充值赠送
    const FROM_RECHARGE_SEND = 8;
    //来源-提现退回
    const FROM_WITHDRAW_BACK = 9;
    //每日任务奖励
    const FROM_DAILY_TASK = 10;
    //收到礼物
    const FROM_GET_GIFT = 11;
    //送出礼物
    const FROM_SEND_GIFT = 12;
    //收到系统礼物
    const FROM_GET_SYSGIFT = 13;
    //来源-视频通话消费
    const FROM_VIDEOCALL_OUT = 14;
    //来源-视频通话收入
    const FROM_VIDEOCALL_IN = 15;
    //天天免费福利
    const FROM_DAILY_FREE = 16;
    //语音随心聊支出
    const FROM_QUICK_VOICE_OUT = 17;
    //语音随心聊收入
    const FROM_QUICK_VOICE_IN = 18;
    //视频随心聊支出
    const FROM_QUICK_VIDEO_OUT = 19;
    //视频随心聊收入
    const FROM_QUICK_VIDEO_IN = 20;
    //来源-邀请奖励（通话收入）
    const FROM_INVITE_INCOME_CALL = 5;
    //来源-邀请奖励（礼物收入）
    const FROM_INVITE_INCOME_GIFT = 21;
    //来源-邀请奖励（充值）
    const FROM_INVITE_RECHARGE = 22;
    //来源-邀请奖励（私聊）
    const FROM_INVITE_PRIVATE_MSG = 23;
    //来源-私信消费
    const FROM_SEND_PRIVATE_MSG_OUT = 24;
    //来源-私信收入
    const FROM_SEND_PRIVATE_MSG_IN = 25;
    //天天福利-充值奖励
    const FROM_DAILY_FREE_RECHARGE = 26;
    //资料完善
    const FROM_PROFILE_FIX = 27;
    //查看微信支出
    const FROM_CHECK_WECHAT_OUT = 28;
    //查看微信收入
    const FROM_CHECK_WECHAT_IN = 29;
    //查看微信退回
    const FROM_CHECK_WECHAT_BACK = 30;
    //许愿活动支出
    const FROM_ACT_MAKE_WISH = 31;
    //许愿树活动-金币中奖收入
    const FROM_ACT_MAKE_WISH_COIN_IN = 32;
    //签到收入
    const FROM_SIGNIN = 33;
    //今日缘分-打招呼
    const FROM_SEND_GREET_MSG_OUT = 34;
    //今日缘分-打招呼
    const FROM_SEND_GREET_MSG_IN = 35;
    //新人专享私信收入
    const FROM_NEW_FREE_MSG_IN = 36;
    //新人指南任务
    const FROM_NEW_GUIDE_TASK = 37;
    //邀约视频
    const FROM_INVITE_VIDEO = 38;
    //女生注册推送礼物
    const FROM_FEMALE_PUSH_GIFT = 39;
    //系统扣币
    const FROM_SYS_COST = 100;
    //系统补币
    const FROM_SYS_GAIN = 101;

    const FROM_DISCOUNT_VIDEO_CALL_IN = 102;
    //来源-视频通话支出
    const FROM_DISCOUNT_VIDEO_CALL_OUT = 103;

    //来源描述
    const FROM_DETAIL = [
        self::FROM_RECHARGE => '充值',
        self::FROM_NEW => '语聊 (新手福利)',
        self::FROM_VOICECALL_OUT => '语音通话支出',
        self::FROM_VOICECALL_IN => '语音通话收入',
        self::FROM_WITHDRAW => '申请提现',
        self::FROM_FIRST_RECHARGE => '首充赠送',
        self::FROM_RECHARGE_SEND => '充值赠送',
        self::FROM_WITHDRAW_BACK => '退回金币 (提现审核不通过)',
        self::FROM_SYS_COST => '系统扣币',
        self::FROM_SYS_GAIN => '系统补币',
        self::FROM_DAILY_TASK => '每日任务奖励',
        self::FROM_GET_GIFT => '收到礼物',
        self::FROM_SEND_GIFT => '送出礼物',
        self::FROM_GET_SYSGIFT => '收到打招呼礼物',
        self::FROM_VIDEOCALL_OUT => '视频通话支出',
        self::FROM_VIDEOCALL_IN => '视频通话收入',
        self::FROM_DAILY_FREE => '天天福利通话',
        self::FROM_QUICK_VOICE_OUT => '语音随心聊支出',
        self::FROM_QUICK_VOICE_IN => '语音随心聊收入',
        self::FROM_QUICK_VIDEO_OUT => '视频随心聊支出',
        self::FROM_QUICK_VIDEO_IN => '视频随心聊收入',
        self::FROM_INVITE_INCOME_CALL => '邀请奖励（通话收入）',
        self::FROM_INVITE_INCOME_GIFT => '邀请奖励（礼物收入）',
        self::FROM_INVITE_RECHARGE => '邀请奖励（充值）',
        self::FROM_INVITE_PRIVATE_MSG => '邀请奖励（私信收入）',
        self::FROM_SEND_PRIVATE_MSG_OUT => '赠送消息',
        self::FROM_SEND_PRIVATE_MSG_IN => '私信收入',
        self::FROM_DAILY_FREE_RECHARGE => '速配通话的充值返利',
        self::FROM_PROFILE_FIX => '资料完善奖励',
        self::FROM_CHECK_WECHAT_OUT => '查看微信支出',
        self::FROM_CHECK_WECHAT_IN => '查看微信收入',
        self::FROM_CHECK_WECHAT_BACK => '查看微信退回',
        self::FROM_ACT_MAKE_WISH => '许愿活动支出',
        self::FROM_ACT_MAKE_WISH_COIN_IN => '许愿活动金币礼物',
        self::FROM_SIGNIN => '签到奖励',
        self::FROM_SEND_GREET_MSG_OUT => '打招呼消息支出',
        self::FROM_SEND_GREET_MSG_IN => '打招呼消息收入',
        self::FROM_NEW_FREE_MSG_IN => '新人专享私信收入',
        self::FROM_NEW_GUIDE_TASK => '新人指南任务奖励',
        self::FROM_INVITE_VIDEO => '视频约会收入',
        self::FROM_FEMALE_PUSH_GIFT => '女生注册送礼',
        self::FROM_DISCOUNT_VIDEO_CALL_IN => '特惠视频收入',
        self::FROM_DISCOUNT_VIDEO_CALL_OUT => '特惠视频支出',
    ];

    public function userItem()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    public function opProfile()
    {
        return $this->belongsTo(UserProfile::class, 'op_user_id', 'user_id');
    }

    public function orders()
    {
        return $this->belongsTo(PayOrder::class, 'op_user_id', 'user_id');
    }

    /**
     * 获取收支明细记录
     */
    static function getDetailList($userId = 0, $page = 0, $pageNum = 20)
    {
        $where = ['user_id' => $userId, 'flag' => 1];
        $start = ($page - 1) * $pageNum;
        return self::where($where)->orderBy('id', 'desc')->offset($start)->limit($pageNum)->get()->toArray();
    }
    /**
     * 构造列表元素
     */
    static function buildItem($item)
    {
        $mark = $item['log_type'] == self::TYPE_CONSUME ? '-' : '+';
        return [
            'title' => self::FROM_DETAIL[$item['from']] ?? '未知',
            'log_time' => (string)$item['created_at'],
            'coin' => "{$mark}{$item['coin']}",
            'total_remain' => $item['remain_income_coin'] + $item['remain_recharge_coin'],
        ];
    }

    /**
     * 添加充值记录
     */
    static function addReCharge(
        $userId = 0,
        $coin = 0,
        $orderId = '',
        $remainCoin = []
    ) {
        $model = new self();
        $model->user_id = $userId;
        $model->coin = $coin;
        $model->order_id = $orderId;
        $model->remain_recharge_coin = $remainCoin['recharge_coin'] ?? 0;
        $model->remain_income_coin = $remainCoin['income_coin'] ?? 0;
        $model->log_type = self::TYPE_RECHARGE;
        $model->from = self::FROM_RECHARGE;
        return $model->saveOrFail() ? 1 : 0;
    }

    /**
     * 添加消费记录
     */
    static function addConsume(
        $userId = 0,
        $coin = 0,
        $from = self::FROM_VOICECALL_OUT,
        $remainCoin = [],
        $orderId = ''
    ) {
        $model = new self();
        $model->user_id = $userId;
        $model->coin = $coin;
        $model->remain_recharge_coin = $remainCoin['recharge_coin'] ?? 0;
        $model->remain_income_coin = $remainCoin['income_coin'] ?? 0;
        $model->from = $from;
        $model->log_type = self::TYPE_CONSUME;
        $model->order_id = $orderId;
        return $model->saveOrFail() ? 1 : 0;
    }

    /**
     * 添加收入记录
     */
    static function addIncome(
        $userId = 0,
        $coin = 0,
        $from = self::FROM_VOICECALL_IN,
        $remainCoin = [],
        $orderId = ''
    ) {
        $model = new self();
        $model->user_id = $userId;
        $model->coin = $coin;
        $model->from = $from;
        $model->remain_recharge_coin = $remainCoin['recharge_coin'] ?? 0;
        $model->remain_income_coin = $remainCoin['income_coin'] ?? 0;
        $model->log_type = self::TYPE_INCOME;
        $model->order_id = $orderId;
        return $model->saveOrFail() ? 1 : 0;
    }
}
